proc TheLast { type } {
	*createmark $type 1 -1
	set id [ hm_getmark $type 1]
	*clearmark $type 1
	return $id
}

proc midsurface { comp } {
	*createmark solids 1 "by collector" $comp
	*midsurface_extract_10 solids 1 3 0 1 11 0 0 20 0 0 10 0 10 -2 undefined 0 0 6
	return [TheLast components]
}

proc BatchMesh { comp } {
	*createmark surfaces 2 "by collector" $comp
	*createstringarray 1 "breakconnectivity 0"
	*hm_batchmesh2 surfaces 2 1 1 "dummy" "dummy"
}


proc MidmeshThickness { elem geo } {
	*createmark components 1 $elem
	*createmark components 2 $geo
	*createstringarray 18 "ThicknessOutputOption : PROPERTYONCOMPONENTS2" "RenameRetainedComponents : 1" \
	  "NeedOffset : 0" "GroupThickness : 1" "ThicknessCorrectionMethod : Interpolation" \
	  "ConnectionInterpolationFactor : 1" "CardImage : SHELLSECTION" "GroupingTolerance : 0.2" \
	  "ThicknessAssignMethod : Average" "RegroupThicknessVariationFactor : 0.2" \
	  "NumDecimals : Automatic" "NumDecimalsOffset : Automatic" "MaxInclinationOfMidMeshWithSolid : 35" \
	  "MaxThicknessGradient : 2" "ThicknessMeasurementLocation : Default" "SkipOutsideMeshFromCorrection : 0" \
	  "MaxRelativeChordalDeviation : 0.01" "SaveFailedToUserMark : 1"
	*compute_midmesh_thickness components 1 components 2 1 18
}

proc delete_geo { comp } {
	eval *createmark components 1 $comp
	*deletemark components 1
}

proc InstancesSyncContents { id } {
	catch {
		*ME_ModuleOccurrenceInstancesSyncContents $id "" "copy_partrep_status=1, excluded_entity_types=props mats, target_outputmark=1, target_mode=3"
		*clearmark modules 1
	}
}

proc remove_holes { comp {maxd 10} } {
	puts "$comp"
	set holes [list]
	hm_holedetectionend
	hm_holedetectioninit
	*createmark surfs 1 "by collector" $comp
	hm_holedetectionsetentities surfs 1
	hm_holedetectionsetholeparams hole_shape=31  max_planar_dim=$maxd
	hm_holedetectionfindholes 1
	set n [hm_holedetectiongetnumberofholes]
	if { $n > 0 } {
		puts "Number of holes = $n"
		for {set i 0} {$i < $n} {incr i} {
			set entity {*}[hm_holedetectiongetholedetailsdata $i entities_list]
			lappend holes [lrange $entity 1 end]
		}
	} else {
		puts  "Holes not detected."
	}
	hm_holedetectionend
	
	*currentcollector components [hm_getcollectorname comps $comp]
	foreach hole $holes {
		eval *createmark lines 1 $hole
		*createbestcirclecenternode lines 1 1 1 0
		*linemarkremovepinholes 1 1
	}
}

proc part_with_hole { args } {
	*createmarkpanel modules 1 "Please select the parts"
	set maxd [ hm_getfloat "max diameter"]
	set part_list [ hm_getmark modules 1 ]
	foreach part $part_list {
		*createmark elems 1 "by module" $part
		if { [llength [hm_getmark elems 1 ]] > 0 } {
			puts "${part} skipped."
			continue
		}
		*createmark comps 1 "by module" $part
		foreach geo [ hm_getmark comps 1 ] {
			puts $geo
			set elem [midsurface $geo]
			remove_holes $elem $maxd
			BatchMesh $elem
			MidmeshThickness $elem $geo
			delete_geo $geo	
		}
		InstancesSyncContents $part
	}
}

proc part_without_hole { args } {
	*createmarkpanel modules 1 "Please select the parts"
	set part_list [ hm_getmark modules 1 ]
	foreach part $part_list {
		*createmark elems 1 "by module" $part
		if { [llength [hm_getmark elems 1 ]] > 0 } {
			puts "${part} skipped."
			continue
		}
		*createmark comps 1 "by module" $part
		foreach geo [ hm_getmark comps 1 ] {
			puts $geo
			set elem [midsurface $geo]
			BatchMesh $elem
			MidmeshThickness $elem $geo
			delete_geo $geo	
		}
		InstancesSyncContents $part
	}
}